<template>
  <div class="check-box-wrapper" @click.stop="checkCurrentBox()">
    <span class="left-check-icon" :class="{'check-left-check-icon':isChecked}"></span>
    <p class="right-text">{{checkDataItem.selectName}}</p>
  </div>
</template>

<script type="text/ecmascript-6">
  import Vue from 'vue'
  import {bus} from '../js/bus.js';
  export default {
    props: {
      checkDataItem: {
        type: Object,
        default: null
      },
      isTotal: {
        type: Boolean,
        default: false
      },
      checked: {
        type: Boolean,
        default: null
      },
      singleEntityChecked: {
        type: Number,
        default: null
      },
      singlehospitalLevel: {
        type: Number,
        default: null
      },
      singlePlaneArea: {
        type: Number,
        default: null
      },
      cancelTotal: {
        type: String,
        default: null
      },
      isBelongToParentWhich: {
        // 如果有父级元素，则设置父级index
        type: Number,
        default: null
      },
      isContrary: {
        // 一级标题是否与二级标题互斥
        type: Boolean,
        default() {
          return false
        }
      },
      isParentLinkChild: {
        // 一级标题是否与二级标题联动
        type: Boolean,
        default() {
          return false
        }
      },
      isParent: {
        // 是否是一级标题
        type: Boolean,
        default() {
          return false
        }
      },
      onlyCode: {
        // 用于标识属于同一个group-list-filter
        type: Number,
        default: null
      },
      isReset: {
        type: Boolean,
        default() {
          return false
        }
      },
      pageChange: {
        type: Number,
        default: null
      },
    },
    data() {
      return {
        isChecked: false
      }
    },
    created() {
      let clickCheckBoxParent = 'clickCheckBoxParent' + this.onlyCode
      bus.$on(clickCheckBoxParent, (isBelongToParentWhich, isChecked) => {
        if (this.isContrary) {
          if (!this.isParent) {
            console.log('子元素的所属父元素：' + isBelongToParentWhich)
            console.log('当前Id:' + this.isBelongToParentWhich)
            if (this.isBelongToParentWhich == isBelongToParentWhich) {
              // 如果有互斥行为，如果是一级标题，如果资源发送的所有父元素ID与当前所属父元素ID一致
              this.checkDataItem.checked = false
              this.isChecked = false
            }
          }
        } else if (this.isParentLinkChild) {
          if (this.isBelongToParentWhich == isBelongToParentWhich) {
            // 如果有联动行为，则父级标题选择，子级标题联动
            this.checkDataItem.checked = isChecked
            this.isChecked = isChecked
          }
        }
      });

      let clickCheckBoxChild = 'clickCheckBoxChild' + this.onlyCode
      bus.$on(clickCheckBoxChild, (isBelongToParentWhich, isChecked) => {
        if (this.isContrary) {
          if (this.isParent) {
            console.log('子元素的所属父元素：' + isBelongToParentWhich)
            console.log('当前Id:' + this.isBelongToParentWhich)
            if (this.isBelongToParentWhich == isBelongToParentWhich) {
              // 如果有互斥行为，如果是一级标题，如果资源发送的所有父元素ID与当前所属父元素ID一致
              this.checkDataItem.checked = false
              this.isChecked = false
            }
          }
        }
      });
    },
    watch: {
      isTotal(val){
        if (val) {
          this.checkDataItem.checked = false;
          this.isChecked = false;
        }
      },
      cancelTotal(val){

      },
      pageChange(){
//        this.getDefault();
      },
      isReset(val){
        if (val) {
          if (this.singlehospitalLevel && this.singlehospitalLevel == 1) {
            this.checkDataItem.checked = true;
            this.isChecked = true;
          } else {
            this.checkDataItem.checked = false;
            this.isChecked = false;
          }
        }
      }
    },
    mounted(){
//      if (this.checked) {
//        this.checkDataItem.checked = true;
//        this.isChecked = true;
//        /*this.$emit('chooseTotal', this.isChecked)*/
//      }
      this.isChecked = this.checkDataItem.checked;
//      console.log('djijifji===' + this.isChecked)
//      if (this.singleEntityChecked && this.singleEntityChecked <= 5) {
//        this.checkDataItem.checked = true;
//        this.isChecked = true;
//      }
//      if (this.singlePlaneArea && this.singlePlaneArea <= 6) {
//        this.checkDataItem.checked = true;
//        this.isChecked = true;
//      }
//      if (this.singlehospitalLevel && this.singlehospitalLevel == 1) {
//        this.checkDataItem.checked = true;
//        this.isChecked = true;
//      }
    },
    methods: {
      checkCurrentBox(){
        console.log(this.checkDataItem)
        //Vue.set(this.checkDataItem, 'checked', true)
        this.checkDataItem.checked = !this.checkDataItem.checked
        this.isChecked = !this.isChecked;
        this.$emit('chooseTotal', this.isChecked)

        if (this.isContrary) {
          // 若有互斥
          // bus发送向所有checkBox发送消息
          if (this.isParent) {
            console.log('点击互斥的checkBox..点击了父级元素')
            let listener = 'clickCheckBoxParent' + this.onlyCode
            bus.$emit(listener, this.isBelongToParentWhich, this.isChecked)
          } else {
            console.log('点击互斥的checkBox..点击了子级元素')
            let listener = 'clickCheckBoxChild' + this.onlyCode
            bus.$emit(listener, this.isBelongToParentWhich, this.isChecked)
          }
          // 添加每次点击check-box，都发送一个消息
          this.$emit('checkListener', this, this.isChecked)
        } else if (this.isParentLinkChild) {
          // 一级标题与二级标题联动，全选则二级子元素全选
          if (this.isParent) {
            console.log('一级标题与二级标题联动的checkBox..点击了父级元素')
            let listener = 'clickCheckBoxParent' + this.onlyCode
            bus.$emit(listener, this.isBelongToParentWhich, this.isChecked)
          } else {
            console.log('一级标题与二级标题联动的checkBox..点击了子级元素')
            let listener = 'clickCheckBoxChild' + this.onlyCode
            bus.$emit(listener, this.isBelongToParentWhich, this.isChecked)
            // 添加每次点击check-box，都发送一个消息
            this.$emit('checkListener', this, this.isChecked)
          }
        } else {
          // 添加每次点击check-box，都发送一个消息
          this.$emit('checkListener', this, this.isChecked)
        }
      },
      setChecked(isCheck) {
        this.checkDataItem.checked = isCheck
        this.isChecked = isCheck
      },
      getDefault(){
        if (this.checked) {
          this.checkDataItem.checked = true;
          this.isChecked = true;
          /*this.$emit('chooseTotal', this.isChecked)*/
        }
        if (this.singleEntityChecked && this.singleEntityChecked <= 5) {
          this.checkDataItem.checked = true;
          this.isChecked = true;
        }
        if (this.singlePlaneArea && this.singlePlaneArea <= 6) {
          this.checkDataItem.checked = true;
          this.isChecked = true;
        } else {
          this.checkDataItem.checked = false;
          this.isChecked = false;
        }
        if (this.singlehospitalLevel && this.singlehospitalLevel == 1) {
          this.checkDataItem.checked = true;
          this.isChecked = true;
        }
      }
    }
  }
</script>

<style>
  .left-check-icon {
    width: 14px;
    height: 14px;
    display: inline-block;
    background-image: url("no_checkmark.png");
  }

  .check-left-check-icon {
    width: 14px;
    height: 14px;
    display: inline-block;
    background-image: url("checkmark.png");
  }

  .right-text {
    display: inline-block;
    margin-left: 6px;
  }

  .check-box-wrapper {
    height: 30px;
    line-height: 30px;
    display: flex;
    align-items: center;
    cursor: pointer;
  }

</style>
